<?php

namespace Luka\NavigationBundle\Model;

class Items implements \Iterator, \ArrayAccess, \Countable
{
    private $collection = array();

    private $position = 0;

    public function addItem($text, $url = "", array $translationParameters = array())
    {
        $this->collection[] = new Item($text, $url, $translationParameters);
        return $this;
    }

    public function addItemWithChildren($text, array $children = array(), $url = "", array $translationParameters = array())
    {
        $this->collection[] = new Item($text, $url, $translationParameters, $children);
        return $this;
    }

    public function addRange(array $items = array()) {
        foreach($items as $item){
            $this->collection[] = $item;
        }
        return $this;
    }

    public function isChildSelected() {
        foreach($this->collection as $item){
            if($item->selected){
                return true;
            }
        }
        return $this;
    }

    public function rewind()
    {
        return reset($this->collection);
    }

    public function current()
    {
        return current($this->collection);
    }

    public function key()
    {
        return key($this->collection);
    }

    public function next()
    {
        return next($this->collection);
    }

    public function valid()
    {
        return key($this->collection) !== null;
    }

    public function offsetExists($offset)
    {
        return isset($this->collection[$offset]);
    }

    public function offsetSet($offset, $value)
    {
        $this->collection[$offset] = $value;
    }

    public function offsetGet($offset)
    {
        return isset($this->collection[$offset]) ? $this->collection[$offset] : null;
    }

    public function offsetUnset($offset)
    {
        unset($this->collection[$offset]);
    }

    public function count()
    {
        return count($this->collection);
    }
}